# -*- mode: snippet -*-
# name: a better makefile template
# expand-env: ((yas/indent-line 'fixed) (yas/wrap-around-region 'nil))
# --

EXECUTABLE := ${1:example-exec}
LIBS       := ${2:rt m}

CFLAGS     := ${3:-g -Wall -O3}
CXXFLAGS   := $(CFLAGS)
CC         := gcc

# You shouldn't need to change anything below this point.
#
SOURCE := $(wildcard *.c) $(wildcard *.cpp)
OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
DEPS := $(patsubst %.o,%.d,$(OBJS))
MISSING_DEPS := $(filter-out $(wildcard $(DEPS)),$(DEPS))
MISSING_DEPS_SOURCES := $(wildcard $(patsubst %.d,%.c,$(MISSING_DEPS)) \
                $(patsubst %.d,%.cpp,$(MISSING_DEPS)))
CPPFLAGS += -MD
RM-F := rm -f
..PHONY : everything deps objs clean veryclean rebuild
everything : $(EXECUTABLE)
deps : $(DEPS)
objs : $(OBJS)
clean :
    @$(RM-F) *.o
    @$(RM-F) *.d
veryclean: clean
    @$(RM-F) $(EXECUTABLE)
rebuild: veryclean everything
ifneq ($(MISSING_DEPS),)
    $(MISSING_DEPS) :
    @$(RM-F) $(patsubst %.d,%.o,$@)
endif
-include $(DEPS)
$(EXECUTABLE) : $(OBJS)
    $(CC) -o $(EXECUTABLE) $(OBJS) $(addprefix -l,$(LIBS))
